/**
 * 
 */
package com.ydy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydy.annotation.CheckFormRepeat;
import com.ydy.annotation.CtrlParam;
import com.ydy.annotation.UserToken;
import com.ydy.controller.base.BaseController;
import com.ydy.exception.BusinessException;
import com.ydy.ienum.EnumSystem;
import com.ydy.model.User;
import com.ydy.service.user.UserService;
import com.ydy.vo.token.TokenVo;
import com.ydy.vo.token.UserTokenVo;

/**
 * @author xuzhaojie
 *
 *         2019年1月18日 上午9:17:29
 */
@Controller
@RequestMapping(value = "user", name = "用户模块")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	/**
	 * 登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@PostMapping(value = "login", name = "用户登录")
	@CheckFormRepeat
	@ResponseBody
	public ResponseEntity<TokenVo> login(@CtrlParam("用户名") String username, @CtrlParam("密码") String password,
			@CtrlParam("验证码") String code, HttpServletRequest request, HttpServletResponse response) {
		if (!compareCode(code, request)) {
			throw new BusinessException(EnumSystem.CODE_ERROR);
		}
		User user = new User(username, password);
		UserTokenVo vo = userService.checkUser(user);
		removeCode(request);
		return ResponseEntity.ok(vo);
	}

	/**
	 * user信息
	 * 
	 * @param response
	 * @return
	 */
	@UserToken
	@GetMapping(value = "info", name = "用户详细信息")
	@ResponseBody
	public ResponseEntity<User> info() {
		return ResponseEntity.ok(userService.selectById(getUser().getId()));
	}

}